package easy;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的中序遍历
 */
public class test10 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode();
        TreeNode treeNode21 = new TreeNode(3);
        TreeNode treeNode22 = new TreeNode();
        TreeNode treeNode2 = new TreeNode(2, treeNode21, treeNode22);
        TreeNode treeNode = new TreeNode(1, treeNode1, treeNode2);
        test10 test10 = new test10();
        System.out.println(test10.inorderTraversal(treeNode));
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        inorder(root, res);
        return res;
    }

    public void inorder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}

